package com.koron.css2.newProjectCenter.impl.print;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjPrintQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ；lwc
 * @Date: 2023/12/26/15:01
 * @Description:
 */
public class PjRegisterPrint implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPrintQuery pjPrintQuery = JsonUtils.objectToPojo(req.getData(), PjPrintQuery.class);

            // 1. 获取打印数据
            ArrayList<HashMap<String, String>> result = getPrintDate(factory, pjPrintQuery,userInfo);
            // 2. 生成打印id
            String key = PrintInterfaceImpl.setPrintData(result);
            List<String> keys = new ArrayList<String>();
            keys.add(key);
            // 3. 返回打印id
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "报装申请登记表获取打印数据成功", List.class);
            messageBean.setData(keys);
            return messageBean;
        } catch (Exception e) {
            logger.error("报装申请登记表获取打印数据失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "报装申请登记表获取打印数据失败", null);
        }
    }

    private ArrayList<HashMap<String, String>> getPrintDate(SessionFactory factory, PjPrintQuery pjPrintQuery, UserInfoBean userInfo) {
        ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
        PjInfoMapper mapper = factory.getMapper(PjInfoMapper.class);
        PjInfoBean pjInfoBean=new PjInfoBean();
        CtmInfoBean ctmInfoBean =new CtmInfoBean();
        MeterBoreBean meterBore =new MeterBoreBean();
        String areaName ="";
        if (StringUtils.isNotBlank(pjPrintQuery.getConsultId())){
            pjInfoBean = mapper.selectById(pjPrintQuery.getConsultId());
            CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
            MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
             ctmInfoBean = ctmMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), CtmInfoBean.CTMTEMP);
            meterBore = meterBoreMapper.selectMeterBoreById(pjInfoBean.getMeterBore());
            areaName = businessAreaMapper.findBusinessAreaById(pjInfoBean.getBusinessArea(), userInfo.getCurWaterCode());
        }

        if (Objects.isNull(ctmInfoBean)){
            ctmInfoBean=new CtmInfoBean();
        }


        HashMap<String, String> hashMap=new HashMap<>();
        hashMap.put("installAddr",Objects.toString(pjInfoBean.getInstallAddr(), ""));
        hashMap.put("ctmName",Objects.toString(ctmInfoBean.getCtmName(), ""));
        hashMap.put("certNo", Objects.toString(ctmInfoBean.getCertNo(), ""));
        hashMap.put("agentMan",Objects.toString(pjInfoBean.getAgentMan(), ""));
        hashMap.put("agentLinkTel",Objects.toString(pjInfoBean.getAgentLinkTel(), ""));
        hashMap.put("areaName", Objects.toString(areaName, ""));
        hashMap.put("meterBore","");
        if (meterBore != null){
            hashMap.put("meterBore", Objects.toString(meterBore.getBoreName(), ""));
        }

        //预留字段
        hashMap.put("billNo",pjInfoBean.getBillNo());
        hashMap.put("pjName",pjInfoBean.getPjName());

        result.add(hashMap);
        return result;
    }
}
